<!DOCTYPE html>
<html lang="zh">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>我的收藏</title>
    <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.4/css/all.min.css">
    <style>
        body {
            background-color: #fff9e6;
            margin: 0;
            padding: 20px;
            font-family: Arial, sans-serif;
        }

        .container {
            max-width: 1200px;
            margin: 0 auto;
        }

        .header {
            display: flex;
            justify-content: space-between;
            align-items: center;
            margin-bottom: 20px;
        }

        .back-btn {
            padding: 8px 16px;
            background-color: #ffd700;
            color: white;
            border: none;
            border-radius: 4px;
            cursor: pointer;
        }

        .sort-options {
            display: flex;
            gap: 10px;
            margin-bottom: 20px;
        }

        .sort-btn {
            padding: 8px 16px;
            background: white;
            border: 1px solid #ffd700;
            border-radius: 4px;
            cursor: pointer;
            transition: all 0.3s;
        }

        .sort-btn.active {
            background: #ffd700;
            color: white;
        }

        .product-grid {
            display: grid;
            grid-template-columns: repeat(auto-fill, minmax(250px, 1fr));
            gap: 20px;
        }

        .product-card {
            background: white;
            border-radius: 8px;
            overflow: hidden;
            box-shadow: 0 2px 8px rgba(0,0,0,0.1);
            transition: transform 0.3s;
            position: relative;
        }

        .product-card:hover {
            transform: translateY(-5px);
        }

        .product-image {
            width: 100%;
            height: 200px;
            object-fit: cover;
        }

        .product-info {
            padding: 15px;
        }

        .product-name {
            font-size: 16px;
            margin-bottom: 10px;
            color: #333;
            display: -webkit-box;
            -webkit-line-clamp: 2;
            -webkit-box-orient: vertical;
            overflow: hidden;
        }

        .product-price {
            color: #ff4400;
            font-size: 18px;
            font-weight: bold;
            margin-bottom: 10px;
        }

        .product-status {
            position: absolute;
            top: 10px;
            right: 10px;
            padding: 5px 10px;
            border-radius: 4px;
            font-size: 12px;
            color: white;
        }

        .status-normal {
            background-color: #52c41a;
        }

        .status-auction {
            background-color: #1890ff;
        }

        .status-auction-upcoming {
            background-color: #722ed1;
        }

        .status-auction-ongoing {
            background-color: #fa8c16;
        }

        .status-auction-ended {
            background-color: #999;
        }

        .product-type {
            position: absolute;
            top: 10px;
            left: 10px;
            padding: 5px 10px;
            border-radius: 4px;
            font-size: 12px;
            color: white;
            background-color: rgba(0, 0, 0, 0.5);
        }

        .product-actions {
            display: flex;
            justify-content: flex-end;
            padding: 10px 15px;
            border-top: 1px solid #eee;
        }

        .action-btn {
            padding: 5px 10px;
            border: none;
            border-radius: 4px;
            cursor: pointer;
            font-size: 14px;
        }

        .view-btn {
            background: #ffd700;
            color: white;
        }

        .remove-btn {
            background: #ff4d4f;
            color: white;
            padding: 5px 15px;
        }

        .empty-state {
            text-align: center;
            padding: 40px;
            color: #666;
        }

        .empty-state i {
            font-size: 48px;
            color: #ddd;
            margin-bottom: 20px;
        }

        .product-image-container {
            position: relative;
            width: 100%;
            height: 200px;
            overflow: hidden;
            transition: opacity 0.3s;
        }

        .product-image-container:hover {
            opacity: 0.9;
        }

        .product-info:hover {
            background-color: #f8f8f8;
        }
    </style>
</head>
<body>
    <div class="container">
        <div class="header">
            <button class="back-btn" onclick="location.href='mine.html'">返回</button>
            <div class="sort-options">
                <button class="sort-btn active" onclick="sortProducts('time')">收藏时间</button>
                <button class="sort-btn" onclick="sortProducts('priceAsc')">价格从低到高</button>
                <button class="sort-btn" onclick="sortProducts('priceDesc')">价格从高到低</button>
            </div>
        </div>

        <div class="product-grid" id="productGrid">
            <!-- 商品将通过JavaScript动态加载 -->
        </div>
    </div>

    <script>
        let products = [];

        function loadFavorites() {
            $.ajax({
                url: '/favorite/list',
                type: 'GET',
                success: function(response) {
                    console.log(response);
                    if (response.state === 1) {
                        products = response.data;
                        renderProducts(products);
                    } else {
                        showEmptyState('加载失败：' + response.msg);
                    }
                },
                error: function() {
                    location.href = 'login.html';
                }
            });
        }

        function renderProducts(products) {
            const grid = $('#productGrid');
            grid.empty();

            if (!products || products.length === 0) {
                showEmptyState('还没有收藏任何商品');
                return;
            }

            products.forEach(product => {
                const isAuction = product.isAuction;
                const productType = isAuction ? '拍卖' : '闲置';
                let statusClass, statusText;

                if (isAuction) {
                    // 拍卖商品状态
                    switch(product.auctionStatus) {
                        case 0:
                            statusClass = 'status-auction-upcoming';
                            statusText = '即将开始';
                            break;
                        case 1:
                            statusClass = 'status-auction-ongoing';
                            statusText = '进行中';
                            break;
                        case 2:
                            statusClass = 'status-auction-ended';
                            statusText = '已结束';
                            break;
                        default:
                            statusClass = 'status-normal';
                            statusText = '未知状态';
                    }
                } else {
                    // 闲置商品状态
                    statusClass = 'status-normal';
                    statusText = '在售';
                }

                const card = `
                    <div class="product-card">
                        <div class="product-image-container" onclick="viewProduct(${isAuction ? product.auctionId : product.productId}, ${isAuction})" style="cursor: pointer;">
                            <img src="${product.productImage}" class="product-image" alt="${product.productName}">
                            <div class="product-type">${productType}</div>
                            <div class="product-status ${statusClass}">${statusText}</div>
                        </div>
                        <div class="product-info" onclick="viewProduct(${isAuction ? product.auctionId : product.productId}, ${isAuction})" style="cursor: pointer;">
                            <div class="product-name">${product.productName}</div>
                            <div class="product-price">￥${product.productPrice}</div>
                        </div>
                        <div class="product-actions">
                            <button class="action-btn remove-btn" onclick="event.stopPropagation(); removeFavorite(${product.productId})">
                                取消收藏
                            </button>
                        </div>
                    </div>
                `;
                grid.append(card);
            });
        }

        function getStatusClass(status) {
            switch(status) {
                case 'normal': return 'status-normal';
                case 'violation': return 'status-violation';
                case 'offline': return 'status-offline';
                default: return '';
            }
        }

        function getStatusText(status) {
            switch(status) {
                case 'normal': return '正常';
                case 'violation': return '商品违规';
                case 'offline': return '已下架';
                default: return '';
            }
        }

        function showEmptyState(message) {
            $('#productGrid').html(`
                <div class="empty-state">
                    <i class="fas fa-heart-broken"></i>
                    <p>${message}</p>
                </div>
            `);
        }

        function sortProducts(type) {
            $('.sort-btn').removeClass('active');
            $(`[onclick="sortProducts('${type}')"]`).addClass('active');

            let sortedProducts = [...products];
            switch(type) {
                case 'time':
                    sortedProducts.sort((a, b) => new Date(b.createTime) - new Date(a.createTime));
                    break;
                case 'priceAsc':
                    sortedProducts.sort((a, b) => a.productPrice - b.productPrice);
                    break;
                case 'priceDesc':
                    sortedProducts.sort((a, b) => b.productPrice - a.productPrice);
                    break;
            }
            renderProducts(sortedProducts);
        }

        function viewProduct(id, isAuction) {
            if (isAuction) {
                window.location.href = `auction-detail.html?id=${id}`;
            } else {
                window.location.href = `product-detail.html?id=${id}`;
            }
        }

        function removeFavorite(productId) {
            if (!confirm('确定要取消收藏吗？')) {
                return;
            }

            $.ajax({
                url: '/favorite/remove',
                type: 'POST',
                data: { productId: productId },
                success: function(response) {
                    if (response.state === 1) {
                        products = products.filter(p => p.productId !== productId);
                        renderProducts(products);
                    } else {
                        alert(response.msg || '取消收藏失败');
                    }
                }
            });
        }

        // 页面加载时获取收藏列表
        $(document).ready(function() {
            loadFavorites();
        });
    </script>
</body>
</html> 